package es.uji.viselab.test;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import javax.media.opengl.*;
import javax.media.opengl.glu.*;
import com.sun.opengl.util.*;

//import com.jogamp.opengl.util.gl2.GLUT;

//import com.sun.opengl.util.*;

// http://stackoverflow.com/questions/2503590/howto-install-jogl-for-eclipse-in-mac-osx-10-6
// https://sites.google.com/site/justinscsstuff/jogl-tutorial-3

/**
 * This program demonstrates a single modeling transformation, glScalef() and a
 * single viewing transformation, gluLookAt(). A wireframe cube is rendered.
 * 
 * @author Kiet Le (Java conversion)
 */
public class cube extends JFrame implements GLEventListener, KeyListener, MouseListener,
		MouseMotionListener {

	private GLUT glut;
	private GLCanvas canvas;
	
	private float view_rotx = 0f, view_roty = 0f, view_rotz = 0.0f;  
	private int prevMouseX, prevMouseY;
	private boolean mouseRButtonDown = false;

	public cube() {
		super("cube");
		//
		canvas = new GLCanvas();
		canvas.addGLEventListener(this);
		canvas.addKeyListener(this);
		//
		getContentPane().add(canvas);
	}

	public void run() {
		setSize(500, 500);
		Frame frame = this;
		setLocationRelativeTo(null);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		 final Animator animator = new Animator(canvas);
		    frame.addWindowListener(new WindowAdapter() {
		        public void windowClosing(WindowEvent e) {
		          // Run this on another thread than the AWT event queue to
		          // make sure the call to Animator.stop() completes before
		          // exiting
		          new Thread(new Runnable() {
		              public void run() {
		                animator.stop();
		                System.exit(0);
		              }
		            }).start();
		        }
		      });
		setVisible(true);
		canvas.requestFocusInWindow();
		animator.start();
	}

	public static void main(String[] args) {
		new cube().run();
	}

	public void init(GLAutoDrawable drawable) {
		GL gl = drawable.getGL();
		glut = new GLUT();
		//
		gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
		gl.glShadeModel(GL.GL_FLAT);
		
		drawable.addMouseListener(this);
		drawable.addMouseMotionListener(this);
	  
		
	}

	/*
	 * Clear the screen. Set the current color to white. Draw the wire frame
	 * cube.
	 */
	public void display(GLAutoDrawable drawable) {
		
		    
		GL gl = drawable.getGL();
		GLU glu = new GLU();
		GLUT glut = new GLUT();
		//
		gl.glClear(GL.GL_COLOR_BUFFER_BIT);
		gl.glColor3f(1.0f, 1.0f, 1.0f);
		gl.glLoadIdentity(); /* clear the matrix */
		
		
		// Rotate the entire assembly of gears based on how the user
        // dragged the mouse around
        //gl.glPushMatrix();
        gl.glRotatef(view_rotx, 1.0f, 0.0f, 0.0f);
        gl.glRotatef(view_roty, 0.0f, 1.0f, 0.0f);
        gl.glRotatef(view_rotz, 0.0f, 0.0f, 1.0f);
        System.out.println(view_rotx + " " + view_roty + " " + view_rotz);
        //gl.glPopMatrix();
        
        /* viewing transformation */
		glu.gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
		gl.glScalef(1.0f, 2.0f, 1.0f); /* modeling transformation */
		glut.glutWireCube(1.0f);
		
        
		gl.glFlush();

		
	}

	/*
	 * Called when the window is first opened and whenever the window is
	 * reconfigured (moved or resized).
	 */
	public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
		GL gl = drawable.getGL();
		//
		gl.glMatrixMode(GL.GL_PROJECTION); /* prepare for and then */
		gl.glLoadIdentity(); /* define the projection */
		gl.glFrustum(-1.0, 1.0, -1.0, 1.0, 1.5, 20.0); /* transformation */
		gl.glMatrixMode(GL.GL_MODELVIEW); /* back to modelview matrix */
		gl.glViewport(0, 0, w, h); /* define the viewport */
	}

	public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) {
	}

	public void keyTyped(KeyEvent arg0) {
	}

	public void keyPressed(KeyEvent key) {
		switch (key.getKeyCode()) {
		case KeyEvent.VK_ESCAPE:
			System.exit(0);
		default:
			break;
		}
	}

	public void keyReleased(KeyEvent arg0) {
	}

	@Override
	public void mouseEntered(MouseEvent e) {
	}

	@Override
	public void mouseExited(MouseEvent e) {
	}

	@Override
	public void mousePressed(MouseEvent e) {
		prevMouseX = e.getX();
		prevMouseY = e.getY();
		if ((e.getModifiers() & e.BUTTON3_MASK) != 0) {
			mouseRButtonDown = true;
		}
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		if ((e.getModifiers() & e.BUTTON3_MASK) != 0) {
			mouseRButtonDown = false;
		}
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		
	}

	// Methods required for the implementation of MouseMotionListener
	@Override
	public void mouseDragged(MouseEvent e) {
		
	//	System.out.println("DRAG !!");
		
		int x = e.getX();
		int y = e.getY();
		Dimension size = e.getComponent().getSize();

		float thetaY = 360.0f * ((float) (x - prevMouseX) / (float) size.width);
		float thetaX = 360.0f * ((float) (prevMouseY - y) / (float) size.height);

		prevMouseX = x;
		prevMouseY = y;

		view_rotx += thetaX;
		view_roty += thetaY;
	}

	@Override
	public void mouseMoved(MouseEvent e) {
	}

}